'use strict';

const xtpl = require('xtpl');
const path = require('path');
const md5 = require('md5');
const captchapng = require('captchapng');
const db = require(path.join(__dirname,'../config/config'));
const CommonHelper = require(path.join(__dirname,"../helpers/commonHelpers"));


module.exports.teacherList = function(req,res){

    //判断是否登录
    CommonHelper.isLogin(req,res);

    //当前页码值
    const page = parseInt(req.query.page || 1);

    //每页显示多少条
    const limit = 10;

    //偏移量
    const start = (page-1)*limit;

    let where = 1;

    db.table('teacher').where(where).count('*').then(function(count){

        //总页数
        const totalPage = count % limit == 0 ? count/limit : parseInt(count/limit)+1;

        const totalPageArray = [];

        //分页
        for(var i=1;i<=totalPage;i++)
        {
            totalPageArray.push(i);
        }

        //查询数据
        db.table('teacher').alias('teacher').join({
            table: 'school',
            join: 'left',
            as: 'school',
            on: ['school_id', 'school_id']
        }).join({
            table: 'user',
            join: 'left',
            as: 'user',
            on: ['user_id', 'user_id']
        }).where(where).order('teacher.teach_id DESC').limit(start,limit).select().then(function(teacher){

            let data = {
                array:teacher,
                count:count,
                totalPageArray:totalPageArray,
                page:page,
                totalPage:totalPage,
            };
            CommonHelper.renderFile(req,res,"teacherList.html",data);
        });
    });
};


module.exports.teacherEdit = function(req,res)
{
    //判断是否登录
    CommonHelper.isLogin(req,res);

    let teach_id = req.query.teach_id;

    if(!teach_id)
    {
        //重定向
        res.redirect(`/teacher/teacherList`);
        return false;
    }

    db.table('teacher').where(`teach_id = ${teach_id}`).find().then(function(teacher){
        db.table('school').select().then(function(school){
            let Data = {
                "teacher":teacher,
                "school":school
            };

            CommonHelper.renderFile(req,res,"teacherEdit.html",Data);
        });
    });
};

module.exports.teacherEditForm = function(req,res)
{
    //判断是否登录
    CommonHelper.isLogin(req,res);

    let teach_id = req.query.teach_id;

    if(!teach_id)
    {
        //重定向
        res.redirect(`/teacher/teacherList`);
        return false;
    }

    db.table('teacher').where(`teach_id = ${teach_id}`).find().then(function(teacher){

        let data = {
            "teach_name":req.body.teach_name,
            "school_id":req.body.school_id
        };

        db.table('teacher').where(`teach_id = ${teach_id}`).update(data).then(function(affectId){
            if(affectId)
            {
                //重定向
                res.redirect(`/teacher/teacherList`);
                return false;
            }else{
                //重定向
                res.redirect(`/teacher/teacherEdit?teach_id=${teach_id}`);
                return false;
            }
        });
    });
};

module.exports.teacherDelete = function(req,res){

    let teach_id = req.query.teach_id;

    if(!teach_id)
    {
        //重定向
        res.redirect(`/teacher/teacherList`);
        return false;
    }

    db.table('teacher').where(`teach_id = ${teach_id}`).find().then(function(teacher){
        if(teacher){
            db.table('user').where(`user_id = ${teacher.user_id}`).delete().then(function(affectId){
                if(affectId){
                    console.log("删除账号表成功");
                    //重定向
                    res.redirect(`/teacher/teacherList`);
                    return false;
                }else{
                    console.log("删除账号表失败");
                    //重定向
                    res.redirect(`/teacher/teacherList`);
                    return false
                }
            });
        }

    })
    
    db.table('teacher').where(`teach_id = ${teach_id}`).delete().then(function(affectId){
        if(affectId){
            console.log("教师表删除成功");
            //重定向
            res.redirect(`/teacher/teacherList`);
            return false;
        }else{
            console.log("教师表删除失败");
            //重定向
            res.redirect(`/teacher/teacherList`);
            return false;
        }
    });
};